﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace BISPOS
{
    public partial class AddNewProductXtraForm : DevExpress.XtraEditors.XtraForm
    {
        BISPOSDataClassesDataContext db = new BISPOSDataClassesDataContext();
        private ProductGroup productGroup;
        private BISPOS.Repository.ProductRepository repository = new Repository.ProductRepository();
        public void ChangeProductGroup(int productGroupId)
        {
            try
            {
                productGroup = repository.GetProductGroup(productGroupId);
                productGroupTextEdit.Text = productGroup.CodeAndName;
            }
            catch
            {
                MessageBox.Show("ไม่พบกลุ่มสินค้า");
            }
        }
        public AddNewProductXtraForm()
        {
            InitializeComponent();
            productGroup = null;
        }

        private void addSimpleButton_Click(object sender, EventArgs e)
        {
            try
            {
                Product product = new Product
                {
                    Barcode = barcodeTextEdit.Text.Trim(),
                    IsTaxed = !isExtVatCheckEdit.Checked,
                    Name = productNameTextEdit.Text.Trim(),
                    Unit = unitTextEdit.Text.Trim(),
                    ProductGroupId = productGroup.Id
                };
                product.Prices.Add(new Price
                {
                    Amount = Convert.ToDecimal(priceTextEdit.Text),
                    StartDate = DateTime.Now,
                    EndDate = null,
                    IsNormal = true,
                    Note = ""
                });
                db.Products.InsertOnSubmit(product);
                db.SubmitChanges();
                this.Close();
            }
            catch
            {
                MessageBox.Show("ป้อนข้อมูลผิดพลาด");
            }
        }

        private void selectProductGroupSimpleButton_Click(object sender, EventArgs e)
        {
            AddNewProductXtraForm_SelectProductGroup form = new AddNewProductXtraForm_SelectProductGroup(this);
            form.Show();
        }

        private void closeSimpleButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}